<?php
declare (strict_types=1);

namespace app\command;


use app\common\model\AmountLog;
use app\common\model\Config;
use app\common\model\DaziJoin;
use app\common\model\MessageTemplate;
use app\common\model\NoticeMessage;
use app\common\model\User;
use app\common\model\UserCommission;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Exception;


class Dazi extends Command
{
    protected function configure()
    {

        $this->setName('dazi')->setDescription('Dazi');
    }

    protected function execute(Input $input, Output $output)
    {
        $daziModel = new \app\common\model\Dazi();
        $daziJoinModel = new DaziJoin();
        $daziList = $daziModel->where(['status' => 2])->where('activity_time', '<', time())->select()->toArray();
      //  $UserCommissionModel = new UserCommission();
        $userModel = new User();
        $amountModel = new  AmountLog();
        $messageModel = new MessageTemplate();
        $xcxMessage = [];
        $xcxDaziUserMessage = [];
        $gzhDaziUserMessage = [];
        $gzhMessage = [];
        foreach ($daziList as $key => $value) {
            $fees = Config::getConfigValue('dazi_fees');
            $recharge_ratio  = Config::getConfigValue('recharge_ratio');
            $userInfo = $userModel->where(['id' => $value['uid']])->find();
            if ($value['people_number'] == $value['participants_number'] || $value['is_start'] == 1) {
                $xcxDaziUserMessage[$key]['data'] = [
                    'thing1' => ['value' => '搭子'],
                    'thing2' => ['value' => $value['title']],
                    'phrase3' => ['value' => '完成'],
                    'time5' => ['value' => date('Y-m-d H:i:s')],
                ];
                $xcxDaziUserMessage[$key]['uid'] = $value['uid'];
                $xcxDaziUserMessage[$key]['title'] = 'release';
                $gzhDaziUserMessage[$key]['data'] = [
                    'thing10' => ['value' => '搭子'],
                    'thing28' => ['value' => $userInfo['nickname']],
                    'thing9' => ['value' => $value['title']],
                    'thing2' => ['value' => '完成'],
                ];
                $gzhDaziUserMessage[$key]['uid'] = $value['uid'];
                $gzhDaziUserMessage[$key]['title'] = 'release';
                try {
                    $daziModel->startTrans();
                    $daziModel->where(['id' => $value['id']])->update(['status' => 5]);
                    $daziJoinModel = new DaziJoin();
                    $totalAmount = $daziJoinModel->where(['dazi_id' => $value['id'],'status' => 1])->sum('price');
                    $daziJoinModel->where(['dazi_id' => $value['id'],'status' => 1])->update(['status' => 5]);
                    $platformAmount = bcdiv(bcmul((string)$totalAmount, (string)$fees, 2), '100', 2);
                    $amount = bcsub((string)$totalAmount, (string)$platformAmount, 2);
                    $userModel->where(['id' => $value['uid']])
                        ->inc('total_income', (float)$amount)
                        ->inc('surplus_income', (float)$amount)
                        ->inc('dazi_income', (float)$amount)
                        ->update();
                    $amountLog = [
                        'uid' => $value['uid'],
                        'amount' => $amount,
                        'amount_type' => 2,
                        'operation_source' => 4,
                        'amount_direction' => 1,
                        'create_time' => time(),
                        'correlation_id' => $value['id'],
                        'ratio' => $fees,
                        'handling_fees_amount' => bcdiv((string)$platformAmount,(string)$recharge_ratio,2),
                        'handling_fees' => $platformAmount,
                        'original_price' =>$totalAmount
                    ];
                    $amountModel->insert($amountLog);

               /*     $UserCommissionModel->addUserCommission($value['uid'], $totalAmount, 1, $value['id']);*/
                    $daziModel->commit();
                } catch (Exception $exception) {
                    $daziModel->rollback();
                    $output->writeln($exception->getMessage());
                }
            } else {
                $daziJoinList = $daziJoinModel->where(['dazi_id' => $value['id'], 'status' => 1])->select()->toArray();
                try {
                    $daziModel->startTrans();
                    $daziModel->where(['id' => $value['id']])->update(['status' => 4, 'participants_number' => 0]);
                    foreach ($daziJoinList as $joinKey => $item) {
                        if ($item['price'] > 0) {
                            $userModel->where(['id' => $item['uid']])->inc('balance', (float)$item['price'])->update();
                            $joinUserInfo = $userModel->where(['id' => $item['uid']])->find();
                            $daziJoinModel->where(['id' => $item['id']])->update(['status' => 4]);
                            $amountData = [
                                'uid' => $item['uid'],
                                'amount' => $item['price'],
                                'amount_type' => 1,
                                'amount_direction' => 1,
                                'create_time' => time(),
                                'correlation_id' => $item['id'],
                                'operation_source' => 3,
                            ];
                            $amountModel->insert($amountData);

                        }
                        $xcxMessage[$joinKey]['data'] = [
                            'thing1' => ['value' => '搭子'],
                            'thing2' => ['value' => $value['title']],
                            'phrase3' => ['value' => '过期'],
                            'time5' => ['value' => date('Y-m-d H:i:s')],
                        ];
                        $xcxMessage[$joinKey]['uid'] = $item['uid'];
                        $xcxMessage[$joinKey]['title'] = 'release';

                        $gzhMessage[$key]['data'] = [
                            'thing10' => ['value' => '搭子'],
                            'thing28' => ['value' => $joinUserInfo['nickname']],
                            'thing9' => ['value' => $value['title']],
                            'thing2' => ['value' => '过期'],
                        ];
                        $gzhMessage[$key]['uid'] = $item['uid'];
                        $gzhMessage[$key]['title'] = 'release';
                    }
                    $xcxDaziUserMessage[$key]['data'] = [
                        'thing1' => ['value' => '搭子'],
                        'thing2' => ['value' => $value['title']],
                        'phrase3' => ['value' => '过期'],
                        'time5' => ['value' => date('Y-m-d H:i:s')],
                    ];
                    $xcxDaziUserMessage[$key]['uid'] = $value['uid'];
                    $xcxDaziUserMessage[$key]['title'] = 'release';
                    $gzhDaziUserMessage[$key]['data'] = [
                        'thing10' => ['value' => '搭子'],
                        'thing28' => ['value' => $userInfo['nickname']],
                        'thing9' => ['value' => $value['title']],
                        'thing2' => ['value' => '过期'],
                    ];
                    $gzhDaziUserMessage[$key]['uid'] = $value['uid'];
                    $gzhDaziUserMessage[$key]['title'] = 'release';
                    $daziModel->commit();


                    //$output->writeln('成功');
                } catch (Exception $exception) {
                    $daziModel->rollback();
                    $output->writeln($exception->getMessage());
                }
            }

        }
        $output->writeln('成功');
        $messageModel->sendMessage(3, $xcxDaziUserMessage);
        $messageModel->sendMessage(3, $xcxMessage);
    }

    /**
     * execute()方法 就是 运行该命令行类的时候要执行的具体业务逻辑代码
     */

}